package com.sporadic.desk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sporadic.desk.dao.MTableDao;
import com.sporadic.desk.pojo.MTable;
import com.sporadic.desk.service.MTableService;
import com.sporadic.desk.service.OrderService;
import dto.OrderDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vo.ResponseData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: sporadic
 * @description:
 * @author: LHD   QQ:1727865856
 * @create: 2019-10-22 15:04
 **/
@Service
public class MTableServiceImpl extends ServiceImpl<MTableDao, MTable> implements MTableService {
    @Autowired
    private MTableDao mTableDao;
    @Autowired
    private OrderService orderService;

    @Override
    public List<MTable> queryTable(Integer mid, Integer tableAreaId) {
        List<MTable> mTables = null;
        try {
            mTables = mTableDao.queryTable(mid, tableAreaId);
        } catch (Exception e) {
            throw new RuntimeException("抱歉服务开小差了！");
        }
        return mTables;
    }

    @Override
    public boolean updateTableStatus(Integer mid, Integer tid, Integer stauts) {
        MTable mTable = new MTable();
        mTable.setDtStatus(stauts);
        //因为是多家门店公用一个系统，所以餐桌编号可能重复但是餐桌id是不会重复的我们根据餐桌编号和餐桌id作为条件来更新餐桌状态
        return super.update(mTable, new QueryWrapper<MTable>().eq("dt_merchant", mid).eq("dt_id", tid));
    }

    @Override
    public boolean updateTableStautsByNum(String dtName, Integer mid, Integer stauts) {
        MTable mTable = new MTable();
        mTable.setDtStatus(stauts);
        //因为是多家门店公用一个系统，所以餐桌编号可能重复但是餐桌id是不会重复的我们根据餐桌编号和餐桌id作为条件来更新餐桌状态
        return super.update(mTable, new QueryWrapper<MTable>().eq("dt_merchant", mid).eq("dt_name", dtName));
    }

    @Override
    public ResponseData<Map<String, Object>> queryTableBymIdAndTableNum(Integer mid, String tableName) {
        MTable table = super.getOne(new QueryWrapper<MTable>().eq("dt_merchant", mid).eq("dt_name", tableName));
        if (table != null) {
            OrderDTO orderDTO = null;
            //如果当前门店对应此餐桌，并别状态为2：就餐||4：点餐->查询当前餐桌订单号，可能会进行加菜;
            if (table.getDtStatus() == 2) {
                orderDTO = orderService.queryBymidANDBytableNum(mid,tableName);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("isNull", false);
            map.put("status", table.getDtStatus());
            map.put("tableId",table.getDtId());
            if (orderDTO!=null){
                map.put("orderId",orderDTO.getOrderId());
            }
            return new ResponseData<>(map);
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("isNull", true);
            map.put("status", "");
            return new ResponseData<>(map, "对不起你扫秒的二维码不正确");
        }
    }

    @Override
    public MTable queryById(Integer id) {
        return super.getById(id);
    }
}
